<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <link rel="stylesheet" href="__CSS__/admin.css" media="all">
    <!--<script src="http://{$Think.config.google_map_url}" async="async"></script>-->
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style>
        .textAlignCenter{
            text-align: left;
        }
        .addroom,.submitBtn{
            float: left;
        }
        #addRoomConfig{
            margin:0 auto;
            width:650px;
        }
    </style>
</head>
<body>
<form class="layui-form" id="addRoomConfig" style="text-align: center;">
    <div class="layui-form-item" style="margin-top:10px">
        <span class="layui-form-item layui-btn addroom" style="margin-top:4px;margin-bottom:0px;float: right;">新增</span>
        <span style="float:left;font-size:18px;line-height: 40px;">酒店房价设置</span>
    </div>
    <table class="layui-table" style="width:650px;" id="roomconfig-table">
        <thead>
        <tr>
            <td class="textAlignCenter">房型</td>
            <td class="textAlignCenter">房间人数</td>
            <td class="textAlignCenter">每人单价</td>
            <td class="textAlignCenter" width="100">操作</td>
        </tr>
        </thead>
        <tbody>
        {if !empty($configList)}
        {foreach name='configList' item='value'}
        <tr>
            <td>
                <input name="room_id" value="{$value.room_id}" type="hidden">
                <input name="room_name" value="{$value.room_name}"  placeholder="请填写房型" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">
            </td>
            <td>
                <input name="room_person_num" value="{$value.room_person_num}"  placeholder="请填写人数" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">
            </td>
            <td>
                <input name="room_price" value="{$value.room_price}"  placeholder="请填写房间价格" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">
            </td>
            <td>
                <!--<span class="layui-btn layui-bg-blue layui-btn-sm uproom" data-id="" style="margin-top: 4px">↑</span>-->
                <span class="layui-btn layui-btn-danger layui-btn-sm delroom" data-id="{$value.room_id}" style="margin-top: 4px"><i class="layui-icon"></i></span>
            </td>
        </tr>
        {/foreach}
        {else}
        <tr>
            <td>
                <input name="room_id" value="" type="hidden">
                <input name="room_name" value=""  placeholder="请填写房型" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">
            </td>
            <td>
                <input name="room_person_num" value=""  placeholder="请填写人数" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">
            </td>
            <td>
                <input name="room_price" value=""  placeholder="请填写房间价格" autocomplete="off" class="layui-input layui-col-xs5" type="text" lay-key="2">
            </td>
            <td>
                <!--<span class="layui-btn layui-bg-blue layui-btn-sm uproom" data-id="" style="margin-top: 4px">↑</span>-->
                <span class="layui-btn layui-btn-danger layui-btn-sm delroom" data-id="" style="margin-top: 4px"><i class="layui-icon"></i></span>
            </td>
        </tr>
        {/if}
        </tbody>
    </table>
    <div class="layui-form-item">
        <button class="layui-btn submitBtn" lay-submit lay-filter="submitRoomConfig">立即提交</button>
    </div>
</form>
<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>
<script>
    layui.use(['layer', 'laydate', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            laydate = layui.laydate,
            form = layui.form;
        //提交表单
        form.on('submit(submitRoomConfig)', function (data) {
            var tr = $("#roomconfig-table tr:not(:first)");
            console.log(tr);
            var post_data = [];
            $.each(tr,function(i,v){
                var room_id = $(v).find("input[name=room_id]").val();
                var room_name = $(v).find("input[name=room_name]").val();
                var room_person_num = $(v).find("input[name=room_person_num]").val();
                var room_price = $(v).find("input[name=room_price]").val();
                if(room_name=="" ||room_person_num=="" || room_price=="" ){
                    layer.msg("请完善所有表格内容！！！");return false;
                }
                var param = {
                    room_id:room_id,
                    room_name:room_name,
                    room_person_num:room_person_num,
                    room_price:room_price,
                    sort_num:i+1
                }
                post_data.push(param);
            })
            var myData = JSON.stringify(post_data)
            console.log(myData);
            $.post("/admin/grouptoursline/modifyRoomConfig?id="+{$_GET['id']},{postdata:myData},function(data){
                layer.msg(data.msg);
                if(data.code==1){
                    setTimeout(function(){
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },1000)
                }
            },"json")
            return false;
        });
    })

    $(".addroom").click(function(){
        var last_tr = $("tbody tr:last-child");
        $("tbody").append("<tr>"+last_tr.html()+"</tr>");
        $("tbody tr:last-child").find("input[name=room_id]").val("");
        $("tbody tr:last-child").find(".delroom").attr("data-id",'');
    })
    $("tbody").on('click','.delroom',function(){
        var tr_num = $("tbody tr");
        if(tr_num.length<=1){
            layer.msg("至少保留一个房型");return false;
        }
        var room_id = $(this).attr("data-id");
        if(room_id=="" || room_id==null || room_id==undefined){
            //直接删除room节点
            $(this).parent().parent().remove();
        }else{
            //请求接口删除room
            var _that = this;
            $.post("/admin/grouptoursline/delRoomConfig",{room_id:room_id},function(data){
                layer.msg(data.msg);
                if(data.code==1){
                    $(_that).parent().parent().remove();
                }
            },'json')
        }
    })
</script>
</body>
</html>